/**
 * UtcDate.java
 */
package fss.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * A date wrapper so that UTC time can be utilized.
 * 
 * @author thinh
 *
 */
public class UtcDate {
    /**
     * The standardize format for a UTC date.
     */
    private static final String DATE_FORMAT = "yyyy.MM.dd:hh.mm.ss.SS";
    /**
     * The date object.
     */
    private Date date = null;
    /**
     * Constructor that will get the current local date in UTC.
     */
    public UtcDate() {
        date = getLocalUtcDate();
    }
    /**
     * Get the utc date.
     * @return the date (UTC).
     */
    public Date getDate() {
        return date;
    }
    @Override
    public String toString() {
        return FssUtils.formatDate(date, DATE_FORMAT);
    }
    /**
     * Get the current local time as UTC.
     * @return the current time in UTC.
     */
    public static Date getLocalUtcDate() {
        Date local = new Date();
        Calendar cal = new GregorianCalendar(TimeZone.getDefault());
        cal.setTime(local);
        long offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
        long minuteOffset = offset / (60 * 1000);
        cal.set(Calendar.MINUTE, (int) (cal.get(Calendar.MINUTE) - minuteOffset));
        return cal.getTime();
    }
}
